package cadc.ping;

import java.util.Iterator;
import java.util.List;

import cadc.actions.UserAction;
import cadc.daos.AccountDao;
import cadc.daos.GroupDao;
import cadc.daos.PingDao;
import cadc.models.Account;

@SuppressWarnings("serial")
public class PreparePing extends UserAction {
	 private String name;
	 private String password;
	 private String pre;
	 private GroupDao groupDao;
	 private AccountDao accountDao;
	 private PingDao pingDao;
	 
	 public String execute(){
		 Account a = accountDao.findAccountByName(name);
		 long pingId = 0;
		 if(a!=null)pingId = a.getId();
		 else{
			 a = new Account();
			 a.setName(name);
			 a.setPassword(password);
			 pingId = groupDao.addGroup(a);
		 }
		 List<Account> as = accountDao.listAccountLike(pre);
		 Iterator<Account> it = as.iterator();
		 while(it.hasNext()){
			 pingDao.addPing(it.next().getId(), pingId);
		 }
		 return SUCCESS;
	 }
	 
	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPre() {
		return pre;
	}
	public void setPre(String pre) {
		this.pre = pre;
	}
	public GroupDao getGroupDao() {
		return groupDao;
	}
	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}
	public PingDao getPingDao() {
		return pingDao;
	}
	public void setPingDao(PingDao pingDao) {
		this.pingDao = pingDao;
	}
}
